package com.briup.cms.web.controller;

import com.briup.cms.bean.Article;
import com.briup.cms.bean.Comment;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.ArticleService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import com.briup.cms.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "Article")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService service;
    //注册一个资讯
    @ApiOperation(value = "注册资讯",notes = "传递一个json对象的字符串")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/saveOrUpdateArticle")
    public Result saveOrUpdateArticle(@RequestBody Article article){
        service.saveOrUpdateArticle(article);
        return Result.success(article);
    }

    //根据资讯id查找具体资讯信息
    @ApiOperation(value = "id查找资讯",notes = "传递一个id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/findArticleById")
    public Result findArticleById(long id){
        Article article = service.findArticleById(id);
        if (article==null){
            throw new ServiceException(ResultCode.Article_NOT_EXIST);
        }
        return Result.success(article);
    }

    //分页查找所有资讯
    @ApiOperation(value = "分页查找所有资讯",notes = "传入第几页")
    @GetMapping("/findArticleAll")
    public Result findArticleAll(Integer pageNum){
        Page<Article> page = service.findAll(pageNum,10);
        return Result.success(page);
    }

    @ApiOperation(value = "通过Id删除对象",notes = "传入要删除对象的Id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @DeleteMapping("/deleteArticleById/{id}")
    public Result deleteArticleById(@PathVariable Long id, HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteArticleById(id);
        return Result.success("单个删除成功");
    }

    @ApiOperation(value = "通过Id批量删除对象",notes = "传入要删除对象的Id的集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/deleteArticleInBatch")
    public Result deleteArticleInBatch(@RequestBody List<Long> ids,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteArticleInBatch(ids);
        return Result.success("批量删除成功");
    }

    @ApiOperation(value = "修改分类status",notes = "传入id和status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/updateArticleStatus")
    public Result updateArticleStatus(Long id,String status,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.updateArticleStatus(id,status);
        return Result.success("update成功");
    }



    @ApiOperation(value = "根据资讯id查找资讯的所有评论，并分页显示评论",notes = "传入页码和每页记录数")
    @GetMapping("/findAllCommentsByArticleId")
    public Result findAllCommentsByArticleId(Long articleId,int pageNum){
        Page<Comment> allCommentsByArticleId = service.findAllCommentsByArticleId(articleId, pageNum, 2);
        return Result.success(allCommentsByArticleId);
    }
}
